<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('manage_login');
        $this->load->model('share_media');
        $this->load->model('manage_comments');
        $this->load->model('manage_follow');
        $this->load->model('get_profile_infos');
        $this->load->model('manage_mag');
        $this->load->model('get_region');
        $this->load->helper('date');
        $fb_config = array(
            'appId'  => '264489850252668',
            'secret' => '150cc65835eb81c20faa114175539758',
            'cookie' => true
        );
        $this->load->library('facebook', $fb_config);
    }

    public function index() {
        $data = $this->manage_login->manageLogin();
        
//        $videos = $this->share_media->getPopVideos();
//        $photos = $this->share_media->getPhotos();
//        $articles = $this->manage_mag->getArticles();
        $data['users'] = $this->get_profile_infos->getUsers();
        $data['regions'] = $this->get_region->getAll();
        $data['videos'] = $this->share_media->getMainVideos();
        $data['articles'] = $this->manage_mag->getArticles();
        $data['media'] = array_merge($data['users'],$data['videos'],$data['articles']);
        
//        $data['articles'] = $articles;
//        $data['photos'] = $photos;
//        $data['videos'] = $videos;
//        foreach ($data['videos'] as $key => $value) {
//            $data['videos'][$key]['video_infos'] = $this->xhrVideo($value['video_id']);
//        }
        $this->load->view('home/home', $data);
    }
    
    function xhrVideo($videoID){
        $video = $this->share_media->getVideo($videoID);
        $user = $this->get_profile_infos->getInfosID($video[0]["user_id"]);
        $likes = $this->getLikes($videoID);
        $favs = $this->getFavs($videoID);
        $didLike = $this->share_media->didLikeVid($videoID,$this->input->cookie('LEAPSESSID'));
        $didFav = $this->share_media->didFavVid($videoID,$this->input->cookie('LEAPSESSID'));
        $comments = $this->manage_comments->getcomments('video',$videoID);
        if($this->input->cookie('LEAPSESSID', TRUE))
            $data['isFollowing'] = $this->manage_follow->isFollowing($this->input->cookie('LEAPSESSID'),$video[0]["user_id"]);
        $data['video'] = $video[0];
        $data['user'] = $user;
        $data['likes'] = $likes;
        $data['favs'] = $favs;
        $data['did_like'] = $didLike;
        $data['did_fav'] = $didFav;
        $data['comments'] = $comments;
        $data['connected'] = $this->input->cookie('LEAPSESSID');
        return $data;
    }
    function getLikes($videoID){
      
      $likes = $this->share_media->getLikesVid($videoID);
      return $likes;
    }
    function getFavs($videoID){
      
      $favs = $this->share_media->getFavsVid($videoID);
      return $favs;
    }
    function search(){
        echo json_encode($this->get_profile_infos->getUsersSearch());
    }
}